import javax.swing.JOptionPane;
/**
 * 
 * Controller of baaad theeengs
 * Generates random events[not yet] and causes damage to the ship 
 * @author Andrew
 *
 */

public class BadThings {

	Ship ship;
	public BadThings(Ship s){
		ship = s;
	}
	
	public Boolean randomEvent(){
		double num = Math.random() *3;
		if (num > 2){
			if(shipRepair()){}
			else if (sickness()){}
			else if (crewDie()){}
			return true;
		}
		else if (num > .2){
			if (sickness()){}
			else if(shipRepair()){}
			else if (crewDie()){}
			return true;
		}
		// end game
		else{
			return false;
		}
	}
	
	
	public Boolean shipRepair(){
		if (ship.inventory.getRepair() < 1){
			return false;
		}
		else{
			ship.inventory.consumeRepair(1);
			double num = Math.random();
			if (num > .2)
				JOptionPane.showMessageDialog(null, "Your ship hit a rock.\n -1 Repair parts");
			else if (num > .4)
				JOptionPane.showMessageDialog(null, "There was a storm last night.\n -1 Repair parts");
			else{
				JOptionPane.showMessageDialog(null, "One of your crew mates was playing with the cannon and damaged your ship.\n -1 Repair parts");
			}
			return true;
		}
	}
	
	public Boolean sickness(){
		if (ship.inventory.getMedicine() < 1){
			return false;
		}
		else{
			ship.inventory.consumeMedicine(1);
			JOptionPane.showMessageDialog(null, "One of your crew mates is sick.\n -1 Medicine");
			return true;
		}
	}
	public Boolean pirateAttack(){
		if (ship.inventory.getRepair() >= 1){
			ship.inventory.consumeRepair(1);
			JOptionPane.showMessageDialog(null, "The pirates attacked your ship.\n -1 Repair parts");
			return true;
		}
		else{
			return crewDie();
		}
	}
	
	public Boolean crewDie(){
		String name = ship.consumeCrewMate();
		// if there are no more crew mates, game over
		if (name == ""){
			return false;
		}
    	JOptionPane.showMessageDialog(null, "The pirates attacked your ship.\n "+ name + " died");
    	return true;
  
	}
}
